package com.zsy.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.zsy.entity.User;
import com.zsy.mapper.IUserMapper;
import com.zsy.service.IUserService;
import com.zsy.utils.JwtUtil;
import com.zsy.utils.Result;
import com.zsy.utils.SendCode;
import com.zsy.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private IUserMapper ium;

    @Override
    public Result login(String phone, String password) {
        if (phone != null && password != null && !phone.isEmpty() && !password.isEmpty()) {
            if (!judgePhone(phone)) {
                return Result.error("手机号格式错误");
            }

            if (!judgePassword(password)) {
                return Result.error("密码格式错误");
            }
            User user = ium.selectUser(phone);
            if (user != null && user.getPassword().equals(password)) {
                Map<String, Object> map = new HashMap<>();
                map.put("phone", user.getPhone());
                map.put("department", user.getDepartment());
                map.put("username", user.getUsername());
//                List<String> aa = new ArrayList<>();
//                aa.add("123");
//                aa.add("321")
//                map.put("user",us);
                String token = JwtUtil.getToken(map);
                UserVO us = new UserVO();
                us.setToken(token);
                us.setUsername(us.getUsername());

                return Result.ok(us);
            }
        }
        return Result.error("用户名或密码错误");
    }

    @Override
    public Result register(User ui) {
        if(judgePhone(ui.getPhone()) && judgePassword(ui.getPassword())){
            ui.setPassword(SecureUtil.md5(ui.getPassword()));
            System.out.println("注册信息："+ui);
            int i = ium.insertUser(ui);
            if (i > 0 ){
                return Result.ok("注册成功");
            }else {
                return Result.error("注册失败");
            }
         }else {
            return Result.error("手机号，或密码不规范");
        }
    }



    //    手机号正则
    @Override
    public boolean judgePhone(String phone) {
//只能输入数字，以一开头第二位数字可以是三到九的任意一个，剩余九位总共十一位数字组成手机号
        String reg = "^1[3456789]\\d{9}$";
         return phone.matches(reg);
    }
// 密码正则
    @Override
    public boolean judgePassword(String password) {
//字母开头（不区分大小写），长度在6~18之间，只能包含字母、数字和下划线
        String regx = "^[a-zA-Z]\\w{5,17}$";
        return password.matches(regx);
    }

    @Autowired
    private SendCode sendCode;

    @Override
    public Result sendCode(String phone) {

        try {
            Boolean code = sendCode.sendSms(phone);
            System.out.println(code);
            if(code == true){
                return Result.ok("发送成功");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return Result.error("发送失败");
    }


}